﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using elibrary.Classes;
using elibrary.Models.Managers;

namespace elibrary.Models.Entities
{
    public class UserOrderItem
    {
        /* Class Fields [Start] ----------------------------------------------------------------- */
        [Key]
        public int id { get; set; }
        public int orderId { get; set; }
        public int bookId { get; set; }
        /* Class Fields [End] ------------------------------------------------------------------- */

        /* Computed Fields [Start] -------------------------------------------------------------- */
        [NotMapped]
        public UserOrder order
        {
            get { return this.getAssociatedOrder(this.orderId); }
            set { this.orderId = value.id; }
        }

        [NotMapped]
        public Book book
        {
            get { return this.getAssociatedBook(this.bookId); }
            set { this.bookId = value.id; }
        }

        public decimal cost
        {
            get { return this.book.price; }
        }
        /* Computed Fields [End] ---------------------------------------------------------------- */

        private UserOrder getAssociatedOrder(int uid)
        {
            UserOrderManager orderManager = new UserOrderManager(AppGlobals.dataContext);
            
            return orderManager.get(uid);
        }

        private Book getAssociatedBook(int uid)
        {
            BookManager bookManager = new BookManager(AppGlobals.dataContext);

            return bookManager.get(uid);
        }
    }
}